package org.kalai.unifypay.tfb.pay;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.bean.TFBAgentPayReqVO;
import org.kalai.unifypay.enums.AcctTypeEnum;
import org.kalai.unifypay.tfb.util.RequestUtil;
import org.kalai.unifypay.util.DateUtil;

/**
 * 天付宝代付
 * 
 * @author nicholas
 *
 */
public class TFBAgentPayService {

	private static final Logger log = Logger.getLogger(TFBAgentPayService.class);

	private static final String AGENT_SINGLE_PAY_URI = "api_pay_single.cgi";

	private static final String TEST_UTL = "http://apitest.tfb8.com/cgi-bin/v2.0/"; // 测试服务地址

	public static Map<String, String> agentPay(TFBAgentPayReqVO agentPayReqVO) {

		Map<String, String> map = new HashMap<String, String>();
		checkParams(map, agentPayReqVO); // 字段检验
		if (map.containsKey("respDesc")) {
			log.info(map);
			map.put("respCode", "01");
			return map;
		}
		// String serviceUrl = Constant.TFB_SERVICE_URL + AGENT_SINGLE_PAY_URI;
		// //正式
		String serviceUrl = TEST_UTL + AGENT_SINGLE_PAY_URI; // 测试
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = format.format(date);

		String acct_name = agentPayReqVO.getAcct_name();
		String memo = agentPayReqVO.getMemo();

		TreeMap<String, String> paramsMap = new TreeMap<String, String>();
		paramsMap.put("version", "1.0"); // 固定填1.0
		paramsMap.put("spid", Constant.TFB_AGENT_PAY_SPID_TEST); // 填写国采分配的商户号
		paramsMap.put("sp_serialno", agentPayReqVO.getSp_serialno()); // 商户交易单号，商户保证其在本系统唯一
		paramsMap.put("sp_reqtime", time); // 系统发送时间，14位固定长度
		paramsMap.put("tran_amt", String.valueOf(agentPayReqVO.getTran_amt())); // 交易金额，单位为分，不带小数点
		paramsMap.put("cur_type", "1"); // 金额类型
		paramsMap.put("pay_type", String.valueOf(agentPayReqVO.getPay_type())); // 普通余额支付填1；垫资代付填3
		paramsMap.put("acct_name", acct_name); // 收款人姓名
		paramsMap.put("acct_id", agentPayReqVO.getAcct_id()); // 收款人账号
		paramsMap.put("acct_type", String.valueOf(agentPayReqVO.getAcct_type())); // 0-借记卡，1-贷记卡，2-对公账号
		paramsMap.put("mobile", agentPayReqVO.getMobile());
		paramsMap.put("bank_name", agentPayReqVO.getBank_name());
		paramsMap.put("bank_settle_no", agentPayReqVO.getBank_settle_no()); // 对私可不值，对公必传
		paramsMap.put("bank_branch_name", agentPayReqVO.getBank_branch_name());
		paramsMap.put("business_type", agentPayReqVO.getBusiness_type());
		paramsMap.put("memo", memo);

		log.info("参数:" + paramsMap);
		map = RequestUtil.sendRequst(serviceUrl, paramsMap);
		log.info("结果:" + map);
		return map;
	}

	/**
	 * 字段检验
	 * 
	 * @param map
	 * @param agentPayReqVO
	 */
	private static void checkParams(Map<String, String> map, TFBAgentPayReqVO agentPayReqVO) {

		if (StringUtils.isBlank(agentPayReqVO.getSp_serialno())) {
			map.put("respDesc", "sp_serialno is required");

		} else if (null == agentPayReqVO.getTran_amt()) {
			map.put("respDesc", "tran_amt is required");

		} else if (StringUtils.isBlank(agentPayReqVO.getAcct_name())) {
			map.put("respDesc", "acct_name is required");

		} else if (StringUtils.isBlank(agentPayReqVO.getAcct_id())) {
			map.put("respDesc", "acct_id is required");

		} else if (null != agentPayReqVO.getAcct_type()
				&& AcctTypeEnum.DuiGong.getType().toString().equals(agentPayReqVO.getAcct_type())
				&& StringUtils.isBlank(agentPayReqVO.getBank_settle_no())) {// 对公账户必传：传开户所在支行的联行号
			map.put("respDesc", "bank_settle_no is required");

		} else if (StringUtils.isBlank(agentPayReqVO.getBusiness_type())) {
			map.put("respDesc", "business_type is required");

		} else if (StringUtils.isBlank(agentPayReqVO.getMemo())) {
			map.put("respDesc", "memo is required");

		}
		if (null == agentPayReqVO.getPay_type()) {
			agentPayReqVO.setPay_type(1); // 付款方式.1-余额支付,2-企业网银,3-垫资支付,缺损值为1
		}
		if (null != agentPayReqVO.getTran_amt()) {
			if (agentPayReqVO.getTran_amt().toString().contains(".")) {
				map.put("respDesc", "tran_amt format is error");
			}
		}

	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		TFBAgentPayReqVO agentPayReqVO = new TFBAgentPayReqVO();
		agentPayReqVO.setAcct_id("6225887805085784");
		agentPayReqVO.setAcct_name("国采支付");
		// agentPayReqVO.setBank_branch_name(bank_branch_name);
		// agentPayReqVO.setBank_name(bank_name);
		// agentPayReqVO.setBank_settle_no(bank_settle_no);
		// agentPayReqVO.setBusiness_no(business_no);
		agentPayReqVO.setBusiness_type("20101");
		agentPayReqVO.setMemo("测试");
		// agentPayReqVO.setMobile(mobile);
		agentPayReqVO.setSp_serialno("123213");//(DateUtil.formatNow("yyyyMMddhhmmss"));
		agentPayReqVO.setTran_amt(12);
		agentPayReqVO.setPay_type(1);
		Map<String, String> map = agentPay(agentPayReqVO);
		System.out.println(map);
	}
}
